package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.clients.GoodsClient;
import com.leyou.page.clients.SpecClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {


    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    public Map<String, Object> getData(Long spuId) {

        Map<String,Object> data = new HashMap<>();

        //根据spuId查询spu，spuDetail，skus

        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);

        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);

        Spu spu = this.goodsClient.querySpuById(spuId);

        data.put("spu",spu);
        data.put("spuDetail",spuDetail);
        data.put("skus",skus);


        Map<Long,String> specs = new HashMap<>();

        //需要根据当前的spuId获取到分类id，根据分类查询所有的特有的规格参数

        List<SpecParam> specParams = this.specClient.querySpecParam(null, spu.getCid3(), null, false);

        specParams.forEach(specParam -> specs.put(specParam.getId(),specParam.getName()));

        data.put("specs",specs);

        List<SpecGroup> specGroups = this.specClient.querySpecGroups(spu.getCid3());

        data.put("specGroups",specGroups);

        return data;
    }
}
